<template>
  <div v-loading="loading" element-loading-text="加载中">
    <el-table :data="mergeList" @selection-change="handleSelectionChange" height="500">
      <el-table-column width="45px" type="selection"></el-table-column>
      <el-table-column width="200px" label="平台订单号" prop="externalOrderNo">
        <template slot-scope="scope">
          <div>{{scope.row.externalOrderNo}}</div>
          <div v-if="scope.row.mainFlag == '1'" class="font-blue">（主订单）</div>
        </template>
      </el-table-column>
      <el-table-column width="190px" label="收货人">
        <template slot-scope="scope">
          <div><span class="font-color">{{ scope.row.customerName }}</span></div>
          <div><span class="font-color">{{ scope.row.customerPhone }}</span></div>
          <div class="font-color line-2 mar-t-2" :title="scope.row.consigneeDetailAddress">{{ scope.row.consigneeDetailAddress }}</div>
        </template>
      </el-table-column>
      <el-table-column width="160px" label="买家信息">
        <template slot-scope="scope">
          <div>昵称：<span class="font-color">{{ scope.row.buyerNick ? scope.row.buyerNick : '--' }}</span></div>
          <div class="flex">
            <div>留言：</div>
            <div class="font-color flex-item line-2 mar-t-2">{{ scope.row.buyerWords ? scope.row.buyerWords : '--' }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="160px" label="卖家备注">
        <template slot-scope="scope">
          <span class="line-2" :title="scope.row.sellerWords">{{ scope.row.sellerWords ? scope.row.sellerWords : '--'  }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160px" label="成交信息">
        <template slot-scope="scope">
          <div>商品数量：<span class="font-color">{{scope.row.goodsNum}}</span></div>
          <div>订单金额：<span class="font-color-ff9">￥{{scope.row.orderAmount}}</span></div>
        </template>
      </el-table-column>
      <el-table-column width="220px" label="时间">
        <template slot-scope="scope">
          <div>创建：<span class="font-color">{{ scope.row.thirdPlatformCreateTime }}</span></div>
          <div>支付：<span class="font-color">{{ scope.row.payTime }}</span></div>
          <div>同步：<span class="font-color">{{ scope.row.syncTime }}</span></div>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="操作" align="right" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.mainFlag != '1'" type="text" size="medium" class="table-btn" @click="doSetMain(scope.row)">设置为主订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 统计块 -->
    <div class="mar-t-10">
      <span>共</span>
      <span class="font-w">{{ externalOrderNos.length }}</span>
      <span>笔，</span>
      <span>当前已选</span>
      <span class="font-red font-w">{{ multipleSelection.length }}</span>
      <span>笔</span>
    </div>

     <!-- 底部按钮区 -->
    <div class="dialog-footer mar-t-20">
      <el-button type="primary" @click="doConfirm">确认合并</el-button>
      <el-button @click="doClose">关闭</el-button>
    </div>

  </div>
</template>

<script>
  import { listThirdDetail, doMergeOrder } from '@/api/goods/thirdOrder'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      },
      platformType: {
        type: Number
      },
      externalOrderNos: {
        type: Array,
        default() {
          return []
        }
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.init()
        }
      }
    },
    data() {
      return {
        // 合并列表
        mergeList: [],
        loading: false,
        multipleSelection: []
      }
    },
    created() {
      this.init()
    },
    methods: {
      // 初始化
      init() {
        const originData = this.$options.data()
        this.mergeList = originData.mergeList
        this.multipleSelection = originData.multipleSelection
        this.loading = true
        const form = {
          enterpriseId: this.getEnterpriseId(),
          externalOrderNos: this.externalOrderNos.join(','),
          platformType: this.platformType
        }
        listThirdDetail(form).then(response => {
          this.loading = false
          if (response.isError) return
          this.mergeList = response.data
        })
      },
      // 取消事件
      doClose() {
        this.$emit('callback', false)
      },
      // 设置主订单事件
      doSetMain(row) {
        this.mergeList.forEach(item => this.$set(item, 'mainFlag', '0'))
        this.$set(row, 'mainFlag', '1')
      },
      // 合并事件
      doConfirm() {
        if (this.multipleSelection.length < 2) {
          this.$message({ message: '请勾选至少两笔数据', type: 'error' })
          return
        }
        const mainList = this.multipleSelection.filter(item => item.mainFlag === '1')
        if (mainList.length !== 1) {
          this.$message({ type: 'error', message: '请设置主订单' })
          return
        }
        this.$confirm('确认合并选中的' + this.multipleSelection.length + '笔订单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          const form = {
            platformType: this.platformType,
            externalOrderNos: this.multipleSelection.map(item => item.externalOrderNo),
            mergeMainOrderNo: mainList[0].externalOrderNo
          }
          doMergeOrder(form).then(response => {
            this.loading = false
            if (response.isError) return
            this.$message({ message: '合并成功', type: 'success' })
            this.$emit('callback', true)
          })
        }).catch(() => {
        })
      },
      // 勾选事件
      handleSelectionChange(val) {
        this.multipleSelection = val
      }
    }
  }
</script>

<style lang="less" scoped>

</style>